//JavaScript代码区域
layui.use(['element', 'table', 'jquery', 'laydate', 'form'], function () {
    var element = layui.element;
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    $(function () {
        tableRender();
        form.render();
    });
    $("#find").click(function () {
        tableRender();
    });
    $("#clear").click(function () {
        $(".search_input").val("");
    })
    function tableRender() {
        table.render({
            elem: '#permission_table'
            // , height: 312
            ,
            url: LOCALHOSTURL + '/permission/permission/getPermissions' //数据接口
            ,
            method: "get",
            xhrFields: {
                withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
            },
            where:{
              "param": $("#param").val()
            },
            headers: {
                "Authorization": $.cookie("token")
            },
            crossDomain: true,
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                ,
                statusCode: 200 //规定成功的状态码，默认：0
            },
            page: true //开启分页
            ,
            title: "权限表",
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [ //表头
                    {
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                    field: 'id',
                    title: 'ID',
                    sort: true,
                    width: 60
                }, {
                    field: 'name',
                    title: '权限名',
                    templet: function (res) {
                        if (res.name == null)
                            return '暂无';
                        return res.name;
                    }
                }, {
                    field: 'value',
                    title: '权值',
                    templet: function (res) {
                        if (res.value == null)
                            return '暂无';
                        return res.value;
                    }
                }, {
                    field: 'uri',
                    title: '权限路径',
                    templet: function (res) {
                        if (res.uri == null)
                            return '暂无';
                        return res.uri;
                    }
                }, {
                    field: 'icon',
                    title: '图标',
                    templet: function (res) {
                        if (res.icon == null || res.icon == '')
                            return '无';
                        else
                            return '<i class="layui-icon '+res.icon+'" width="30px" height="30px"/>';
                    }
                }, {
                    field: 'parentName',
                    title: '父级权限',
                    templet: function (res) {
                        if (res.parentId != 0 && res.parentId != null) {
                            if (res.parentName == null)
                                return '无';
                            return res.parentName;
                        }else
                            return '无'
                    }
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 100
                }
                ]
            ],
            done: function (res) {
                sessionStorage.setItem("permission", JSON.stringify(res.data));
            }
        });
    }

    table.on('toolbar(demo)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                layer.open({
                    type: 2,
                    title: "添加权限",
                    area: ['100%', '100%'],
                    content: ["edit_permission.html"],
                    shadeClose: true, //点击旁边地方自动关闭
                    //动画
                    anim: 0,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                        var iframeWin = layero.find('iframe')[0].contentWindow;
                        console.log(iframeWin);
                        iframeWin.editPermission("",index);
                    }
                });
                break;
        }
        ;
    })
    ;

//监听行工具事件
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        console.log(obj);
        if (obj.event === 'del') {
            layer.confirm("确定要删除" + data.name + "?", function (index) {
                // obj.del();
                let result = myAjax("/permission/permission/deletePermission", "get", {id: data.id});
                alert(result.message);
                tableRender();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: "修改权限",
                area: ['100%', '100%'],
                content: ["edit_permission.html"],
                shadeClose: true, //点击旁边地方自动关闭
                //动画
                anim: 0,
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                    var iframeWin = layero.find('iframe')[0].contentWindow;
                    console.log(iframeWin);
                    iframeWin.editPermission(JSON.stringify(data),index);
                }
            });
        }
    })
})
;
